package com.zengde.ai_sandbox.service.strategy.impl;

import com.zengde.ai_sandbox.context.OrderContext;
import com.zengde.ai_sandbox.context.OrderContextHolder;
import com.zengde.ai_sandbox.entity.Order;
import com.zengde.ai_sandbox.service.strategy.PaymentStrategy;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service("wechatPayment")
public class WechatPaymentStrategy extends AbstractPaymentStrategy{
    @Override
    public String getType() {
        return "WECHAT";
    }
    
    // 1. 检查订单状态是否合法
    private void checkOrderStatus() {
        OrderContext orderContext = OrderContextHolder.getOrderContextTL();
        Order order = orderContext.getOrder();
        
        // 检查订单是否存在且支付金额大于0
        if (order == null || order.getPaymentAmount() == null || order.getPaymentAmount().compareTo(BigDecimal.ZERO) <= 0) {
            throw new IllegalStateException("订单状态不合法: 订单为空或支付金额不正确");
        }
        
        System.out.println("订单状态检查通过: 订单号=" + order.getOrderNo() + ", 支付金额=" + order.getPaymentAmount());
    }
    
    // 2. 调用远程接口
    public void invokeRmtInterface() {
        OrderContext orderContext = OrderContextHolder.getOrderContextTL();
        Order order = orderContext.getOrder();
        
        // 模拟调用微信支付接口
        System.out.println("正在调用微信支付接口: 订单号=" + order.getOrderNo() + ", 支付金额=" + order.getPaymentAmount());
        
        // 模拟网络延迟
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        
        // 模拟生成支付ID
        String paymentId = "wx_" + System.currentTimeMillis();
        orderContext.setPaymentId(paymentId);
        orderContext.setActualPaymentAmount(order.getPaymentAmount());
        
        System.out.println("微信支付接口调用成功: paymentId=" + paymentId);
    }
    
    // 微信支付特殊逻辑
    public void executePaymentSpecialLogic() {
        OrderContext orderContext = OrderContextHolder.getOrderContextTL();
        Order order = orderContext.getOrder();
        
        System.out.println("执行微信支付特殊逻辑: 订单号=" + order.getOrderNo());
        
        // 微信支付特殊处理逻辑
        // 例如：生成微信支付二维码、检查微信支付配置等
        System.out.println("1. 生成微信支付二维码");
        System.out.println("2. 验证微信支付配置");
        System.out.println("3. 检查微信支付限额");
        
        // 模拟特殊逻辑处理时间
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        
        System.out.println("微信支付特殊逻辑执行完成");
    }
    
    // 3. 更新订单状态
    private void updateOrderStatus() {
        OrderContext orderContext = OrderContextHolder.getOrderContextTL();
        Order order = orderContext.getOrder();
        
        System.out.println("开始更新订单状态: 订单号=" + order.getOrderNo());
        
        // 模拟更新订单状态
        order.setPaymentType("WECHAT");
        
        System.out.println("订单状态更新完成: 订单号=" + order.getOrderNo() + ", 支付方式=微信支付, 实际支付金额=" + orderContext.getActualPaymentAmount());
    }
    
    @Override
    public void process() {
        // 1. 检查订单状态是否合法
        checkOrderStatus();
        
        // 2. 调用远程接口
        invokeRmtInterface();

        // 2.1 执行微信支付特殊的逻辑
        executePaymentSpecialLogic();
        
        // 3.更新订单状态
        updateOrderStatus();

    }
}